﻿using OccurrencesWeb.Page;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OccurrencesCore.Model;
using OccurrencesCore.Service;

namespace OccurrencesWeb.Pages.Occurrence
{
    public partial class AddForm : ExtendedPageBase
    {
        #region PROPERTIES

        private int typeId
        {
            get
            {
                return CheckAndGetTypeIdQueryString();
            }
            set { }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            // Populate fields
            PopulateReasonsDropDown();
            PopulateCarriersDropDown();
            PutOccurrenceDateForToday();
            PopulateDistributionCenterDropDown();
        }

        #region POPULATE FIELDS

        private void PopulateReasonsDropDown()
        {
            ddlReason = GetSelectOneMessageForFropDowns(ddlReason);
            new OccurrenceReasonService().FindByType(typeId).ForEach(reason => 
                
                ddlReason.Items.Add(new ListItem
                {
                    Text = reason.Name,
                    Value = reason.Id.ToString()
                })
            );
            ddlReason.DataBind();
        }

        private void PopulateCarriersDropDown()
        {
            ddlCarriers = GetSelectOneMessageForFropDowns(ddlCarriers);
            new CarrierService().FindAll().ForEach(carrier =>
                ddlCarriers.Items.Add(new ListItem
                {
                    Text = carrier.Name,
                    Value = carrier.Id.ToString()
                })
            );
            ddlCarriers.DataBind();
        }

        private void PopulateDistributionCenterDropDown()
        {
            ddlDistributionCenter = GetSelectOneMessageForFropDowns(ddlDistributionCenter);
            new DistributionCenterService().FindAll().ForEach(distributionCenter =>
                ddlDistributionCenter.Items.Add(new ListItem
                {
                    Text = distributionCenter.Name,
                    Value = distributionCenter.Id.ToString()
                })
            );
            ddlCarriers.DataBind();
        }

        private void PutOccurrenceDateForToday()
        {
            txtOccurrenceDate.Text = DateTime.Now.ToString("MM/dd/yyyy");
        }

        private int CheckAndGetTypeIdQueryString()
        {
            if (!string.IsNullOrEmpty(Page.RouteData.Values["typeId"] as string))
            {
                int typeId;
                if (int.TryParse(Page.RouteData.Values["typeId"] as string, out typeId))
                    return typeId;
                else
                    return int.MinValue;
            }
            else
                return int.MinValue;
        }

        #endregion
    }
}